// echo aux function only, should not compile alone.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using HarmonyLib;
using static Utils.CharacterMod;
namespace DevilNest; // 这里使用DevilNest作为类名，因为这里的年轻需要以增长真实年龄为代价，而年老时候需要设定生时为当前月+1，因此这里的Utils跟真正的Utils略有不同


public static partial class Utils {
    public static FieldInfo _birthMonth=typeof(GameData.Domains.Character.Character).GetField("_birthMonth",(BindingFlags)(-1));
    public static FieldInfo _actualAge=typeof(GameData.Domains.Character.Character).GetField("_actualAge",(BindingFlags)(-1));
    public static FieldInfo _currAge=typeof(GameData.Domains.Character.Character).GetField("_currAge",(BindingFlags)(-1));
    public static uint offset__birthmonth=unchecked((uint)(-1));
    public static uint offset__actualAge =unchecked((uint)(-1));
    public static uint offset__currAge   =unchecked((uint)(-1));
    public static ushort FieldName2FieldId_BirthMonth=GameData.Domains.Character.CharacterHelper.FieldName2FieldId["BirthMonth"];
    public const short lifeMax=11107; // 8295 * 100+服芝食玉100+服食仙丹50+天元一气15+仙胎仙骨15+天人骨血15=295，11107*295/100=32765 //好生寿长100不算，但这里不计算。
    public unsafe static void GetOffset(){
        EarlyCheck();
    }
    public unsafe static void younger(this GameData.Domains.Character.Character victim, int delta, sbyte month, GameData.Common.DataContext context){ // 人，年轻月数，当前月份，context
        // 小学奥数比高中奥数难多了
        // 你永远不知道你什么时候会不小心翻车
        var birth=(sbyte)_birthMonth.GetValue(victim);
        var remain=((month-birth+12)%12);
        int delta_age=(remain - delta +1200000)/12 - 100000; // 避免向0取整带来的误差
        var curr=(sbyte)((birth+delta) % 12);

        if(victim.GetCurrAge()+delta_age<16){
            short life=victim.GetBaseMaxHealth();
            if(life<=8295 || !victim.GetFeatureIds().Contains(261)){ // 好生寿长
                victim.SetBaseMaxHealth((short)(Math.Min(Math.Max(lifeMax,life),life+((month-curr+12)%12))), context);
            }
            curr=month;
            delta_age=16-victim.GetCurrAge();
        }
        if(delta_age!=0){
            victim.SetCurrAge((short)(victim.GetCurrAge()+delta_age),context);
        }
        if(delta_age!=-1){
            victim.SetActualAge((short)(victim.GetActualAge()+delta_age+1),context);
        }
        victim.birthMonth(curr, context);
    }
    public unsafe static void older(this GameData.Domains.Character.Character victim, sbyte month, GameData.Common.DataContext context){ // 人，当前月份，context，在使用之后会因为过月直接变老
        victim.birthMonth(month, context);
    }
}
